<?php

if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
 	require_once (APPLICATION_PATH . "\\models\\connectionDb.php");
} else {
    require_once (APPLICATION_PATH . "//models//connectionDb.php");
}

class Application_Model_DbTable_Groupmembership extends Zend_Db_Table_Abstract
{

    protected $_name = 'Group_Membership';
    protected $_primary = 'id';
    
    
	public function getGroupMembershipUser($User_id, $group_id)
    {
    	try{
	    	$User_id = (int)$User_id;	    	
			$row = $this->fetchRow('User_id = ' . $User_id . ' and Group_id='.$group_id);
			if (!$row) {
				return null;
			}
			return $row->toArray();
    	}
    	catch (Zend_Exception $e) {  
			echo "Error message: " . $e->getMessage() . "\n";
        } 
    }
    
    public function getAllGroupsForUser($userID)
    {
    	try{
	    	$userID = (int)$userID;
	    	$rows = $this->fetchAll("User_id = " .$userID);
	    	
	    	if (!$rows) {
					return null;
				}
				return $rows;
    	}
    	catch (Zend_Exception $e) {  
			echo "Error message: " . $e->getMessage() . "\n";
        }
    }
    
	public function AddGroupMembership($membershipType,$User_id, $Group_id)
    {
    	try{
	    	$data = array(
	    		'MemebershipType' => $membershipType,
	    		'User_id' => $User_id,
	    		'Group_id' => $Group_id,	    		
	    	);
	    	$this->insert($data);
    	}
    	catch (Zend_Exception $e) {  
			echo "Error message: " . $e->getMessage() . "\n";
        } 
    }
	public function deleteGroupMemberShip($User_id,  $group_id)
    {
    	try{
    		$this->delete('User_id = ' . (int)$User_id. ' and Group_id='.$group_id);
    	}
    	catch (Zend_Exception $e) {  
			echo "Error message: " . $e->getMessage() . "\n";
        } 
    }


}

